package com.lhf.spareClass.service.impl;


import com.lhf.enumfile.IsNot;
import com.lhf.enumfile.Status;
import com.lhf.error.ErrorMessageVo;
import com.lhf.legalPlant.model.FactoryVo;
import com.lhf.spareClass.mapper.SpareMapper;
import com.lhf.spareClass.model.*;
import com.lhf.spareClass.service.SpareService;
import com.lhf.util.AllrecordsVo;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-25 22:50
 */
@Service
public class SpareServiceImpl implements SpareService {



    @Autowired
    private SpareMapper spareMapper;
    @Override
    public ResultVo likeCode(SpareLikeQuery spareLikeQuery) {
        List<SpareVo>  spareVo =spareMapper.likeCode(spareLikeQuery);
        return ResultBuliderVo.success(spareVo);
    }

   @Override
    public ResultVo queryByCode(String code) {
        SpareVo  spareVo=spareMapper.queryByCode(code);
        return ResultBuliderVo.success(spareVo);
    }

    @Override
    public ResultVo subClass() {
        List<SubClassVo> subClassVo  = spareMapper.subClass();
        return ResultBuliderVo.success(subClassVo);
    }

  @Override
    public ResultVo queryPage(SpareSelectVo spareSelectVo) {
        List<SpareVo>  list =spareMapper.queryPage(spareSelectVo);
        Integer count = spareMapper.queryCount();
      for(SpareVo spareVo:list){
          spareVo.setEnableName(Status.getStatusName(spareVo.getEnable()));
          spareVo.setBorrowName(IsNot.getStatusName(spareVo.getBorrow()));
          spareVo.setMarketName(IsNot.getStatusName(spareVo.getMarket()));
      }
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setTotalSize(count);
        allrecordsVo.setPageIndex(spareSelectVo.getPageIndex());
        allrecordsVo.setPageSize(spareSelectVo.getPageSize());
        allrecordsVo.resettotalNum(count);
        allrecordsVo.setDataList(list);
        return ResultBuliderVo.success(allrecordsVo);
    }

    @Override
    public ResultVo update( SpareUpdateVo spareUpdateVo) {

        SpareVo spareVo = new SpareVo();
        BeanUtils.copyProperties(spareUpdateVo,spareVo);
        System.out.println(spareVo.toString());
        Date now = new Date();
        spareVo.setUpdateName("李四");
        spareVo.setUpdateTime(now);
        int a = spareMapper.update(spareVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    public ResultVo add(@Valid SpareAddVo spareAddVo) {
        //判断法人是否已经创建完备件阀值
        int count =spareMapper.selectByCode(spareAddVo.getCode(),spareAddVo.getLegalPersonCode());
        if(count!=0){
            return ResultBuliderVo.error("当前法人以对该备件加过阀值");
        }

        SpareVo spareVo = new SpareVo();
        BeanUtils.copyProperties(spareAddVo,spareVo);
        spareVo.setCreateName("张三");
        Date now = new Date();
        spareVo.setCreateTime(now);
        spareVo.setUpdateName("张三");
        spareVo.setUpdateTime(now);
        int a = spareMapper.add(spareVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    public ResultVo queryByLegalPersonCode(String legalPersonCode) {
       List<SpareQueryVo> list =  spareMapper.queryByLegalPersonCode(legalPersonCode);
        return ResultBuliderVo.success(list);
    }
    @Override
    public ResultVo queryByLegalPersonCode2(String legalPersonCode) {
        List<SubClassQueryVo> subClassQueryVos = spareMapper.queryByLegalPersonCode2(legalPersonCode);
        return ResultBuliderVo.success(subClassQueryVos);
    }

    @Override
    public ResultVo select(String legalPersonCode) {
        List<SpareQueryVo> subClassQueryVos =spareMapper.select(legalPersonCode);
        return ResultBuliderVo.success(subClassQueryVos);
    }

}
